package utils

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

//ResponseData  通用返回结构体
type ResponseData struct {
	Code ResCode     `json:"code"`
	Msg  interface{} `json:"msg"`
	Data interface{} `json:"data"`
}

//ResponseSuccess  内置的成功状态码
func ResponseSuccess(c *gin.Context, data interface{}) {
	//1、用gin.H
	//res := gin.H{
	//	"code": code,
	//	"msg":  getMsg(code),
	//	"data": nil,
	//}
	res := &ResponseData{
		Code: CodeSuccess,
		Msg:  CodeSuccess.getMsg(),
		Data: data,
	}
	c.JSON(http.StatusOK, res)
}

//ResponseError  内置的错误状态码
func ResponseError(c *gin.Context, code ResCode) {
	res := &ResponseData{
		Code: code,
		Msg:  code.getMsg(),
		Data: nil,
	}
	c.JSON(http.StatusOK, res)
}

//ResponseErrorWithMsg  自定义不存在的错误状态码及其Msg提示
func ResponseErrorWithMsg(c *gin.Context, code ResCode, msg interface{}) {
	res := &ResponseData{
		Code: code,
		Msg:  msg,
		Data: nil,
	}
	c.JSON(http.StatusOK, res)
}
